/*
* @Author: z.p
* @Project: Zdotp
* @Date: 2024年12月30日
* @Description: 管理日志对象的创建和logo打印
*/

using NLog;
using NLog.Targets;

namespace Zdotp.Logs;

public static class LogHelper
{
    public enum Level
    {
        Info = 0,
        Warn = 1,
        Error = 2,
        Fatal = 3 
    }

    private static LogLevel GetNLogLevel(Level level)
    {
        switch (level)
        {
            case Level.Info:
                return LogLevel.Info;
            case Level.Warn:
                return LogLevel.Warn;
            case Level.Error:
                return LogLevel.Error;
            case Level.Fatal:
                return LogLevel.Fatal;
        }
        return LogLevel.Trace;
    }
    
    public static ZLogger CreateLogger(string loggerName)
    {
        var zyLogger = new ZLogger(loggerName);
        return zyLogger;
    }

    public static NLog.Config.LoggingConfiguration CreateConsoleLogConfig(string loggerName, Level minLevel, Level maxLevel)
    {
        var config = new NLog.Config.LoggingConfiguration();
        var consoleFile = new ConsoleTarget($"console-{loggerName}");
        consoleFile.Layout = "${longdate}  |  ${level:uppercase=true}  | [ ${logger} ] |  ${message}";
        config.AddTarget(consoleFile);
        config.AddRule(GetNLogLevel(minLevel), GetNLogLevel(maxLevel), consoleFile);
        return config;
    }
    
    public static NLog.Config.LoggingConfiguration CreateFileLogConfig(string loggerName, Level minLevel, Level maxLevel)
    {
        var config = new NLog.Config.LoggingConfiguration();
        var fileTarget = new FileTarget($"file-{loggerName}")
        {
            FileName = "logs/${shortdate}.log",
            Layout = "${longdate}  |  ${level:uppercase=true}  | [ ${logger} ] |  ${message}"
        };
        config.AddTarget(fileTarget);
        config.AddRule(GetNLogLevel(minLevel), GetNLogLevel(maxLevel), fileTarget);
        return config;
    }

    private static void PrintLine(int stringLength, int maxLen)
    {
        var len = stringLength * maxLen + 2 * (stringLength - 1) + 8;
        Console.WriteLine(new string('-', len));
    }
    public static void PrintBigMessage(string message)
    {
        // 定义每个字符的图案
        var patterns = new Dictionary<char, string[]>
        {
            {'A', new string[] {"  *  ", " * * ", "*****", "*   *", "*   *"}},
            {'B', new string[] {"**** ", "*   *", "**** ", "*   *", "**** "}},
            {'C', new string[] {" ****", "*    ", "*    ", "*    ", " ****"}},
            {'D', new string[] {"**** ", "*   *", "*   *", "*   *", "**** "}},
            {'E', new string[] {"*****", "*    ", "**** ", "*    ", "*****"}},
            {'F', new string[] {"*****", "*    ", "***  ", "*    ", "*    "}},
            {'G', new string[] {" *** ", "*   *", "*   *", "*  **", " *** "}},
            {'H', new string[] {"*   *", "*   *", "*****", "*   *", "*   *"}},
            {'I', new string[] {"*****", "  *  ", "  *  ", "  *  ", "*****"}},
            {'J', new string[] {"  ** ", "   * ", "   * ", "*  * ", " *** "}},
            {'K', new string[] {"*   *", "*  * ", "**   ", "* *  ", "*   *"}},
            {'L', new string[] {"*    ", "*    ", "*    ", "*    ", "*****"}},
            {'M', new string[] {"*   *", "** **", "* * *", "*   *", "*   *"}},
            {'N', new string[] {"*   *", "**  *", "* * *", "*  **", "*   *"}},
            {'O', new string[] {" *** ", "*   *", "*   *", "*   *", " *** "}},
            {'P', new string[] {"**** ", "*   *", "**** ", "*    ", "*    "}},
            {'Q', new string[] {" *** ", "*   *", "*  **", "* * *", " ** *"}},
            {'R', new string[] {"**** ", "*   *", "**** ", "* *  ", "*   *"}},
            {'S', new string[] {" *** ", "*    ", " *** ", "    *", " *** "}},
            {'T', new string[] {"*****", "  *  ", "  *  ", "  *  ", "  *  "}},
            {'U', new string[] {"*   *", "*   *", "*   *", "*   *", " *** "}},
            {'V', new string[] {"*   *", "*   *", " * * ", "  *  ", "  *  "}},
            {'W', new string[] {"*   *", "*   *", "* * *", "** **", "*   *"}},
            {'X', new string[] {"*   *", " * * ", "  *  ", " * * ", "*   *"}},
            {'Y', new string[] {"*   *", " * * ", "  *  ", "  *  ", "  *  "}},
            {'Z', new string[] {"*****", "   * ", "  *  ", " *   ", "*****"}}
        };

        const int maxLines = 5; // 假设每个字符的图案最多有 5 行
        
        PrintLine(message.Length, maxLines);
        
        for (var i = 0; i < maxLines; i++)
        {
            Console.Write("|  ");
            foreach (char c in message.ToUpper())
            {
                if (patterns.ContainsKey(c))
                {
                    if (i < patterns[c].Length)
                    {
                        Console.Write(patterns[c][i]);
                    }
                    else
                    {
                        // 如果该字符的图案没有足够的行，补充空格
                        Console.Write(new string(' ', patterns['A'][i].Length));
                    }
                }
                else
                {
                    // 如果字符不在字典中，补充空格
                    Console.Write(new string(' ', patterns['A'][i].Length));
                }
                Console.Write("  ");
            }
            
            Console.Write("  |");
            Console.WriteLine();
        }
        
        PrintLine(message.Length, maxLines);
    }
}